package com.ruoyi.develop.lvRuku.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvRuku.domain.LvRuku;
import com.ruoyi.develop.lvRuku.service.ILvRukuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入库单Controller
 *
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/lvRuku/lvRuku")
public class LvRukuController extends BaseController {
    @Autowired
    private ILvRukuService lvRukuService;

    /**
     * 查询入库单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvRuku entity) {
        startPage();
        List<LvRuku> list = lvRukuService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出入库单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:export')")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvRuku entity) {
        List<LvRuku> list = lvRukuService.selectList(entity);
        ExcelUtil<LvRuku> util = new ExcelUtil<LvRuku>(LvRuku.class);
        util.exportExcel(response, list, "入库单数据");
    }

    /**
     * 获取入库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvRukuService.selectById(id));
    }

    /**
     * 新增入库单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:add')")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvRuku entity) {
        return toAjax(lvRukuService.insert(entity));
    }


    /**
     * 新增入库单  带明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:add')")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping("/addWithDetail")
    public AjaxResult addWithDetail(@RequestBody LvRuku entity) {
        return toAjax(lvRukuService.insertWithDetail(entity));
    }


    /**
     * 修改入库单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:edit')")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvRuku entity) {
        return toAjax(lvRukuService.update(entity));
    }


    /**
     * 修改入库单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:edit')")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PutMapping("/editWithDetail")
    public AjaxResult editWithDetail(@RequestBody LvRuku entity) {
        return toAjax(lvRukuService.updateWithDetail(entity));
    }


    /**
     * 删除入库单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:remove')")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvRukuService.deleteByIds(ids));
    }


    /**
     * 采购到货的 一键出库
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRuku:query')")
    @GetMapping(value = "/oneKeyBuyOut/{id}")
    public AjaxResult oneKeyBuyOut(@PathVariable("id") String id) {
        LvRuku lvRuku = lvRukuService.selectById(id);
        if (lvRuku == null) {
            return AjaxResult.error("入库单不存在");
        }
        lvRukuService.oneKeyBuyOut(lvRuku);
        return AjaxResult.success(lvRukuService.selectById(id));
    }


}
